from rest_framework.response import Response

from apis.app01.models import Teacher, Goods, Cattention
from apis.user.models import User


def teacher_user(tid):
    if not tid:
        return Response({"error_code": 400, "message": "参数不全"})
    data = {}
    teacher = Teacher.objects.filter(id=tid).first()  # 存不存在不愿展示信息
    data['cattention_nums'] = teacher.cattention_nums
    data['caller_nums'] = teacher.caller_nums
    user = User.objects.filter(id=teacher.uid, status=1).first()
    data['img'] = user.img
    data['nickname'] = user.img
    data['goods'] = []
    goods = Goods.objects.filter(tid=teacher.id)
    for i in goods:
        res = {}
        res['goods_title'] = i.title
        res['goods_brief'] = i.brief
        data['goods'].append(res)
    return Response({"error_code": 200, "message": "数据获取成功", "data": data})


def cattention(uid,tid):
    if not uid:
        return Response({"error_code": 400, "message": "token已失效"})
    if not tid:
        return Response({"error_code": 400, "message": "参数不全"})
    try:
        Cattention.objects.create(tid=tid, uid=uid)
        teacher = Teacher.objects.filter(id=tid).first()
        teacher.cattention_nums += 1
        teacher.save()
        return Response({"error_code": 200, "message": "用户关注成功"})
    except:
        return Response({"error_code": 404, "message": "用户关注失败"})